using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;


namespace AspNetForums.Admin
{
	/// <summary>
	/// Summary description for BanControl.
	/// </summary>
	public class BanControl : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlInputButton BanUser;
		protected System.Web.UI.WebControls.TextBox UserName;
		protected System.Web.UI.WebControls.Literal results;
		protected System.Web.UI.HtmlControls.HtmlInputButton RemoveBan;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			Components.User user = Users.GetUser();
			if( user != null )
			{
				if( !user.IsAdministrator )
				{
					HttpContext.Current.Response.Redirect( Components.Globals.GetSiteUrls().Home, true );
				}
			}
			else
			{
				HttpContext.Current.Response.Redirect( Components.Globals.GetSiteUrls().Home, true );
			}
			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.BanUser.ServerClick += new System.EventHandler(this.BanUser_ServerClick);
			this.RemoveBan.ServerClick += new System.EventHandler(this.RemoveBan_ServerClick);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void BanUser_ServerClick(object sender, System.EventArgs e)
		{
			string username = this.UserName.Text.Trim();
			//AspNetForums.Components.User user = Users.FindUserByUsername(username);

			if (Users.GetUserAccountStatus(username) != AspNetForums.Enumerations.UserAccountStatus.Banned)
			{
				Users.BanUser(username);
				if (Users.GetUserAccountStatus(username) == AspNetForums.Enumerations.UserAccountStatus.Banned)
				{
					results.Text="<p>User Banned</p>";
				}
				else
				{
					results.Text="<p>Ban Failed</p>";
				}

			}
			else
			{
				results.Text="<p>User is already banned!</p>";
			}

			
		}

		private void RemoveBan_ServerClick(object sender, EventArgs e)
		{
			string username = this.UserName.Text.Trim();
			//AspNetForums.Components.User user = Users.FindUserByUsername(username);

			if (Users.GetUserAccountStatus(username) == AspNetForums.Enumerations.UserAccountStatus.Banned)
			{
				Users.RemoveBan(username);
				
				if (Users.GetUserAccountStatus(username) == AspNetForums.Enumerations.UserAccountStatus.Approved)
				{
					results.Text="<p>User Ban Removed</p>";
				}
				else
				{
					results.Text="<p>Ban Removal Failed</p>";
				}

			}
			else
			{
				results.Text="<p>User is not currently banned</p>";
			}


		}
	}
}
